package com.cct.digitalarchapi.common;

import com.cct.digitalarchapi.dao.Log4operateMapper;
import com.cct.digitalarchapi.pojo.Log4operate;
import com.cct.digitalarchapi.pojo.dto.SshConnection;
import com.jcraft.jsch.ChannelExec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @FileName CommonUtil
 * @Description 工具类
 * @Author Created by chenmingzhe
 * @Date 2024/1/2
 **/
@Slf4j
@Component
public class CommonUtil {
    @Autowired
    private Log4operateMapper log4operateMapper;

    public void recordOperateLog(String content) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String staffCode=request1.getHeader("staffCode");
        String recContent="";
        if(StringUtils.isNotBlank(staffCode)){
            recContent="用户" + staffCode + content;
        }else {
            recContent= content;
        }
        Log4operate log4operate=new Log4operate();
        log4operate.setContent(recContent);
//        String ip=getIp(request1);
//        log4operate.setIpAddress(ip);
        log4operate.setOperator(staffCode);
        log4operateMapper.addLog4operate(log4operate);
    }

    public String listToString(List<String> list){

        StringBuffer sb = new StringBuffer();
        for (String s : list) {
            sb.append("'").append(s).append("'").append(",");
        }
        String res="("+sb.toString().substring(0, sb.toString().length() - 1)+")";
       return  res;
    }


    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        // 多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ip != null && ip.length() > 15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }



}
